//
//  LBRightTableView.m
//  loveBeen
//
//  Created by guofei on 2016/11/19.
//  Copyright © 2016年 loveBeen. All rights reserved.
//

#import "LBRightTableView.h"

#import "LBGetDataTool.h"


static NSString *RIGHTCELL = @"rightcell";

@interface LBRightTableView ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, assign) NSInteger newSection;



@end

@implementation LBRightTableView

- (instancetype)init
{
    self = [super init];
    if (self) {

        [self observerNotification];
        
        [self setupUI];
    }
    return self;
}

- (void)setMarketModelList:(NSArray<LBSuperMarketCategory *> *)marketModelList{

    _marketModelList = marketModelList;
    
    [self reloadData];

}

// 监听通知
- (void)observerNotification{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveSection:) name:RowNum object:nil];

}

// 接收到通知后执行的方法
- (void)receiveSection:(NSNotification *)userInfo{

    NSDictionary *dict = userInfo.userInfo;
    
    NSNumber *section = dict[RowNum];
    
    self.newSection = [section integerValue];
    
    [self reloadData];
    
    NSIndexPath *idxPath = [NSIndexPath indexPathForRow:0 inSection:0];
    
    // 点击左侧 cell 之后,跳转到右侧 cell  时,右侧 cell 回滚到 0 组 0 行
    [self scrollToRowAtIndexPath:idxPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}



#pragma mark - 设置界面
- (void)setupUI{
    
    // 注册 cell
    [self registerNib:[UINib nibWithNibName:@"LBRightCell" bundle:nil] forCellReuseIdentifier:RIGHTCELL];
    // 代理
    self.dataSource = self;
    self.delegate = self;
    // 行高
    self.rowHeight = 80;
    
    // 设置 footerview
    [self setFooterView];
    
    self.sectionHeaderHeight = 30;
    
}

// footerview
- (void)setFooterView{

    self.tableFooterView = [[[UINib nibWithNibName:@"RightFooterView" bundle:nil] instantiateWithOwner:nil options:nil] lastObject];

}

#pragma mark - datasource
// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.marketModelList[self.newSection].productsModelList.count;
    
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    LBRightCell *rightCell = [self dequeueReusableCellWithIdentifier:RIGHTCELL forIndexPath:indexPath];
  
    rightCell.model = self.marketModelList[self.newSection].productsModelList[indexPath.row];
    
    rightCell.productCount = rightCell.model.totalCount;
    
    return rightCell;
}

#pragma mark -  组标题
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UILabel *headerLabel = [[UILabel alloc]init];
   
    headerLabel.text = [NSString stringWithFormat:@"   %@",self.marketModelList[self.newSection].name];
    
    headerLabel.font = [UIFont systemFontOfSize:15];
    
    headerLabel.textColor = [UIColor colorWithWhite:0.2 alpha:1];
    
    headerLabel.textAlignment = NSTextAlignmentLeft;
    
    headerLabel.backgroundColor = [UIColor cz_colorWithHex:0xf5f5f5];
    
    return headerLabel;
}


// 通过代理(控制器)去 push 到 detailVC 详情页
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LBSuperMarketProducts *model = self.marketModelList[self.newSection].productsModelList[indexPath.row];

    if ([_pushDelegate respondsToSelector:@selector(pushToDetailVC:withProductModel:)]) {
        
        [_pushDelegate pushToDetailVC:self withProductModel:model];
    }

    
}





// 移除通知
- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
